package defiant.common.web;

import static defiant.common.tools.SysProperties.DEBUG;

import java.util.Properties;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.velocity.app.Velocity;

import defiant.common.tools.Logger;
import defiant.common.tools.ResourceUtil;

/**
 * Servlet implementation class VelocityServlet
 */
public class VelocityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		
		// default
		Velocity.setProperty("resource.loader", "webapp");
		Velocity.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader");

		Properties props = ResourceUtil.loadProperties("resources.velocity");
		try {
			Velocity.setProperty("webapp.resource.loader.path", props.getProperty("webapp.resource.loader.path"));
			if (DEBUG) Logger.log("Velocity path -->" + Velocity.getProperty("webapp.resource.loader.path") + "<--");
		} catch (Exception e) {
			Logger.log(new Exception("Velocity property file not found", e));
		}

		Velocity.setApplicationAttribute("javax.servlet.ServletContext", getServletContext());
		if (DEBUG) Logger.log("-- Velocity initialized --"); 
	}

}
